#!/bin/bash
#################################################################
# @Description: 用例名称：虚拟机cpu资源隔离，测试目的：虚拟机可以拥有独立的物理资源，且各个虚拟机之间严格隔离
# @Date : 2023-11-24
# @Last Modified by :
# @Last Modified time: 2023-11-27
#################################################################
source "../common.sh"
export LANG=en_US.UTF-8
res=0
\cp ../xml/*.xml /opt/ 
vm3_name="vm3"
vm4_name="vm4"
cleanup_environment > /dev/null 2>&1
# 启动虚拟机 vm3，vm4
vm3_xml="/opt/vm3.xml"
vm4_xml="/opt/vm4.xml"

echo "检查$vm3_name CPU和I/O线程绑定配置："
cat $vm3_xml|grep "<vcpupin vcpu='0' cpuset='4'/>" || res=1
cat $vm3_xml|grep "<vcpupin vcpu='1' cpuset='6'/>" || res=1
cat $vm3_xml|grep "<vcpupin vcpu='2' cpuset='8'/>" || res=1
cat $vm3_xml|grep "<vcpupin vcpu='3' cpuset='10'/>" || res=1
cat $vm3_xml|grep "iothread" || res=1
[ "$res" -eq 0 ] && echo "检查$vm3_name CPU和I/O线程绑定成功" || { echo "检查$vm3_name CPU和I/O线程绑定失败"; exit 1; }
echo "检查$vm4_name CPU和I/O线程绑定配置："
cat $vm4_xml|grep "<vcpupin vcpu='0' cpuset='5'/>" || res=1
cat $vm4_xml|grep "<vcpupin vcpu='1' cpuset='7'/>" || res=1
cat $vm4_xml|grep "<vcpupin vcpu='2' cpuset='9'/>" || res=1
cat $vm4_xml|grep "<vcpupin vcpu='3' cpuset='11'/>" || res=1
cat $vm4_xml|grep "iothread" || res=1
[ "$res" -eq 0 ] && echo "检查$vm4_name CPU和I/O线程绑定成功" || { echo "检查$vm4_name CPU和I/O线程绑定失败"; exit 1; }

echo "创建并启动虚拟机$vm3_name $vm4_name"
virsh define "$vm3_xml" >/dev/null || { res=1; echo "$vm3_name define failed"; exit 1; }
virsh start "$vm3_name" >/dev/null || { res=1; echo "$vm3_name start failed"; exit 1; }
virsh define "$vm4_xml" >/dev/null || { res=1; echo "$vm4_name define failed"; exit 1; }
virsh start "$vm4_name" >/dev/null || { res=1; echo "$vm4_name start failed"; exit 1; }
vnc_port3=$(virsh vncdisplay $vm3_name)
vnc_port4=$(virsh vncdisplay $vm4_name)
sleep 20
# 循环查询虚拟机的 IP 地址
echo "获取 VM3 和 VM4 IP 地址中·····"
while true; do
    ip_vm3=$(get_vm_ip "$vm3_name")
    ip_vm4=$(get_vm_ip "$vm4_name")
    #获取到IP，跳出循环
    if [ -n "$ip_vm3" ] && [ -n "$ip_vm4" ]; then
        break  # 退出循环
    fi
    sleep 10
done

# 检查并 ping 的 IP 地址
check_and_ping $ip_vm3 >/dev/null && echo "虚拟机$vm3_name:$ip_vm3启动正常！"
check_and_ping $ip_vm4 >/dev/null && echo "虚拟机$vm4_name:$ip_vm4启动正常！"
sshpass -p "$password" scp -o StrictHostKeyChecking=no -q cpu_task.py $ip_vm3:/home > /dev/null 2>&1 && echo -e "\033[32m-----------------测试脚本已上传至$ip_vm3----------------\033[0m"|| res=1
sshpass -p "$password" scp -o StrictHostKeyChecking=no -q cpu_task.py $ip_vm4:/home > /dev/null 2>&1 && echo -e "\033[32m-----------------测试脚本已上传至$ip_vm4----------------\033[0m"|| res=1
sshpass -p "$password" ssh -o StrictHostKeyChecking=no -q $ip_vm3 "chmod a+x /home/cpu_task.py" > /dev/null 2>&1
sshpass -p "$password" ssh -o StrictHostKeyChecking=no -q $ip_vm4 "chmod a+x /home/cpu_task.py" > /dev/null 2>&1
# 输出最终状态

# 判断用例是否执行成功
[ "$res" = "0" ] && echo -e "\033[32m-----------------vm3 vm4虚拟机已创建且测试脚本已上传成功，剩余测试步骤，请手动执行。----------------\033[0m" || echo -e "\033[31m-----------------自动创建虚拟机失败，请排查或手动执行该测试用例----------------\033[0m"
[ "$res" = "0" ] && echo "
远程连接虚拟机，虚拟机vm3 IP:$ip_vm3 、vm4 IP:$ip_vm4;虚拟机root用户密码：$password。
运行测试脚本，在虚拟机任意目录下执行 python /home/cpu_task.py
测试步骤如下：
2.只在虚拟机vm3上执行以上任务，记录虚拟机的压力进程结束的运行耗时。
3.只在虚拟机vm4上执行以上任务，记录虚拟机的压力进程结束的运行耗时。
4.在两个虚拟机vm3，vm4上同时执行以上任务，记录两个虚拟机的压力进程结束的运行耗时。
预期结果：
1.步骤2、3、4所有耗时差在5%以内。 
"
